//
//  WDMineLogoutTipView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/16.
//

#import "WDMineLogoutTipView.h"
#import "UIWindow+WDMineLogoutTipView.h"


@interface WDMineLogoutTipView ()

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) void(^completionBlock)(void);
@property (nonatomic, copy) void(^failedBlock)(void);

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *descLabel;


@property (nonatomic, strong) UIButton *btnLeft;

@property (nonatomic, strong) UIButton *btnRight;



- (void)initWithTitleLabel;

- (void)initWithDescLabel;

- (void)initWithBtnLeft;

- (void)initWithBtnRight;

@end

@implementation WDMineLogoutTipView

- (void)initWithSubView
{
    self.backgroundColor = kWhiteColor;
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
    self.clipsToBounds = YES;
    
    [self initWithTitleLabel];

    [self initWithDescLabel];
    
    [self initWithBtnLeft];

    [self initWithBtnRight];
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(20, 20, self.width - 40, 25);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    
    self.titleLabel.text = objectNull(self.title);
}

- (void)initWithDescLabel {
    if (!_descLabel) {
        CGRect frame = CGRectMake(24, self.titleLabel.bottom + 10, self.width - 48, 44);
        _descLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"确认要退出登录？退出登陆后如需要再次使用APP则需要重新登录。";
            label.numberOfLines = 2;
        }];
    }
}

- (void)initWithBtnLeft {
    if (!_btnLeft) {
        CGRect frame = CGRectMake(18, self.height - 36 - 18, (self.width - 18 * 2 - 20) * 0.5, 36);
        _btnLeft = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(clickedCancel:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"取消" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
            button.layer.cornerRadius = frame.size.height * 0.5;
            button.layer.masksToBounds = YES;
        }];
    }
}

- (void)initWithBtnRight {
    if (!_btnRight) {
        CGRect frame = self.btnLeft.frame;
        frame.origin.x = self.btnLeft.right + 20;
        _btnRight = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(clickedSubmit:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"确定" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#19C46E"];
            button.layer.cornerRadius = frame.size.height * 0.5;
            button.layer.masksToBounds = YES;
        }];
    }
}



- (void)showWithTitle:(NSString *)title data:(id)data completion:(void (^)(void))completionBlock failed:(void (^)(void))failedBlock
{
    self.title = title;
    self.completionBlock = completionBlock;
    self.failedBlock = failedBlock;
    if (!kCSKeyWindow.logoutTipView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.logoutTipMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.logoutTipView = self;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedSubmit:)];
//        [maskView addGestureRecognizer:tap];
//        [self addGestureRecognizer:tap];
        
        [UIView animateWithDuration:0.2 animations:^{
            maskView.alpha = 0.4;
            self.alpha = 1;
        }];
    }
    [self initWithTitleLabel];
}

- (void)clickedCancel:(id)sender
{
    if (self.failedBlock)
    {
        self.failedBlock();
    }
}

- (void)clickedSubmit:(id)sender
{
    if (self.completionBlock)
    {
        self.completionBlock();
    }
}

- (void)hide
{
    if (kCSKeyWindow.logoutTipView)
    {
        @weakify(self);
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            self.alpha = 0;
            kCSKeyWindow.logoutTipMaskView.alpha = 0;
        } completion:^(BOOL finished) {
//            @strongify(self);
//            if (self.completionBlock)
//            {
//                self.completionBlock(nil);
//            }
            [kCSKeyWindow.logoutTipView removeFromSuperview];
            [kCSKeyWindow.logoutTipMaskView removeFromSuperview];
            kCSKeyWindow.logoutTipMaskView = nil;
            kCSKeyWindow.logoutTipView = nil;
        }];
    }
}

@end
